// __PUBLIC__/js/page/admin/circle/circlelist
require(['../../common'], function() {
  require(['circle.circlelist'])
})

define('circle.circlelist', [
  'jquery',
  'page/grid'
], function($, Grid) {
  'use strict';
  var $table = $('.table')
  var mygrid = new Grid($table)
  mygrid.init()

  function getSelected() {
    var selected = mygrid.getSelected()
    if (!selected.count) {
      alert(lang.pleaseSelectAData)
      return
    }
    return selected
  }

  function getASelected(t) {
    var current = mygrid.getCurrent(t)
    var selected = {
      id: [current.id],
      index: [current.index]
    }
    console.log(selected)
    return selected
  }

  $(document)
    .on('click', '.A_del', function(e) {
      e.preventDefault()
      if(confirm("确定删除吗？")){
        deleteCircle(getASelected(this))
      }
    })
    .on('click', '#J_del', function(e) {
      e.preventDefault()
      var selected = getSelected()
      if(confirm("确定删除吗？")){
        deleteCircle(selected)
      }
    })
    .on('click', '#J_search', function(e) {
      e.preventDefault()
      searchData()
    });

    $('.A_top').on('click',function(e){
        e.preventDefault()
        var val = $(this).attr('data-val'),
            id = $(this).attr('data-id');
        changeState('totop',id,val,this);
    });
    $('.A_homepage').on('click',function(e){
        e.preventDefault()
        var val = $(this).attr('data-val'),
            id = $(this).attr('data-id');
        changeState('tohomepage',id,val,this);
    });
    
    function changeState(type,id,val,obj){
        var data = {id:id,val:val,type:type};
        $.post(APP + '?app=admin&m=Circle&a=toTop',data,function(e){
            if(1 == e){
//                location.reload();
                var text = '';
                if('totop' == type){
                    if(1 == val){
                        text = "取消置顶";
                    }else{
                        text = "置顶";
                    }
                }else{
                    if(1 == val){
                        text = "取消推荐到首页";
                    }else{
                        text = "推荐到首页";
                    }
                }
                $(obj).attr('data-val',1-val);
                $(obj).html(text);
            }else{
                alert('操作失败');
            }
        });
    }

  function deleteCircle(selected) {
    $.post(APP + '?app=admin&m=Circle&a=doDeleteCircle', {
      ids: selected.id.join()
    })
      .done(function(res) {
        if (res == '0') {
          alert(lang.delFails)
        } else {
          /*$('#sysdeleteuser').html(res)*/
//          alert(lang.delSuccess)
          mygrid.del(selected.index)
        }
      })
  }

  function searchData() {
//    var circlename = $('#circlename').val()
//    var par_get = '&circlename=' + circlename
//    location.href = APP + '?app=admin&m=Circle&a=circlelist' + par_get
        $('#search').submit();
  }

})
